package com.socialcoco.config;


import com.socialcoco.auth.AuthFilter;
import com.socialcoco.auth.AuthRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager")
                                                                     DefaultWebSecurityManager
                                                                     defaultWebSecurityManager){
        ShiroFilterFactoryBean FilterFactoryBean = new ShiroFilterFactoryBean();
        FilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        //设置可以游客访问的路径
        Map<String,String> filterMap=new LinkedHashMap<>();
        filterMap.put("/socialcoco/user/checkName/*","anon");
        filterMap.put("/socialcoco/user/enroll","anon");
        filterMap.put("/socialcoco/user/login","anon");
        filterMap.put("/socialcoco/user/checkPassword/**","anon");
        filterMap.put("/socialcoco/user/checkEmail/**","anon");
        filterMap.put("/socialcoco/user/slow/*","anon");
        filterMap.put("/socialcoco/user/sms/*","anon");
        filterMap.put("/socialcoco/user/sms/OAuth/login/success","anon");
        filterMap.put("/imserver/*","anon");
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/socialcoco/user/killIndex/**","anon");//测试并发
        filterMap.put("/socialcoco/user/spikeProducts","anon");
        filterMap.put("/socialcoco/user/uploadimg","anon");
        filterMap.put("/socialcoco/user/uploadimg/*","  anon");
        filterMap.put("/druid/**", "anon");
        filterMap.put("/doc.html","anon");
        filterMap.put("/swagger-ui/**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/**/v3/api-docs", "anon");
        filterMap.put("/swagger/**", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/doc.html", "anon");
        // 除了以上路径，其他都需要权限验证
        filterMap.put("/**", "auth");
        FilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        //设置自定义的shiro过滤器
        Map<String, Filter> filters = new HashMap<>();
        filters.put("auth", new AuthFilter());
        FilterFactoryBean.setFilters(filters);
        return FilterFactoryBean;

    }
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(AuthRealm authRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(authRealm);
        return defaultWebSecurityManager;
    }

    @Bean
    @DependsOn({"lifecycleBeanPostProcessor"})
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }
    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor()
    { return new LifecycleBeanPostProcessor(); }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
}
